package codingformat.wizards;

import java.io.File;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.eclipse.core.internal.resources.Folder;
import org.eclipse.core.resources.IProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.internal.core.CompilationUnit;
import org.eclipse.jdt.internal.core.PackageFragment;

public class BeanUtils {

	// 创建domain的Dao tempPath模板文件的路径 createFilePath创建文件路径
	public static PackageFragment createDaoBean(String tempPath,
			PackageFragment packageName, IProject project,
			CompilationUnit domain, String templateName) throws Exception {
		String rootPath = project.getLocation().toFile().getParent();
		String domainName = domain.getElementName();
		domainName = domainName.substring(0, domainName.indexOf('.'));
		String createFilePath = rootPath
				+ packageName.getPath().toFile().getPath() + File.separator
				+ domainName + "Dao.java";
		Template.TemplateToFile(tempPath, createFilePath,
				packageName.getElementName(), null, null, null, null, domain,
				templateName,null);
		return packageName;
	}

	// 创建domain的DaoImpl tempPath模板文件的路径 createFilePath创建文件路径
	public static IPackageFragment createDaoImplBean(String tempPath,
			IPackageFragmentRoot packageFragmentRoot, IProject project,
			PackageFragment packageName, CompilationUnit domain,
			String templateName) throws Exception {
		String rootPath = project.getLocation().toFile().getParent();
		String domainName = domain.getElementName();
		domainName = domainName.substring(0, domainName.indexOf('.'));
		String createFilePath = rootPath
				+ packageName.getPath().toFile().getPath() + File.separator
				+ "impl" + File.separator + domainName + "DaoImpl.java";
		IPackageFragment imp = packageFragmentRoot.createPackageFragment(
				packageName.getElementName() + ".impl", true, null);
		Template.TemplateToFile(tempPath, createFilePath, imp.getElementName(),
				packageName.getElementName(), null, null, null, domain,
				templateName,null);
		return imp;
	}

	// 创建domain的Service tempPath模板文件的路径 createFilePath创建文件路径
	public static void createServiceBean(String tempPath, IProject project,
			PackageFragment packageName, PackageFragment daoName,
			CompilationUnit domain, String templateName) throws Exception {
		String rootPath = project.getLocation().toFile().getParent();
		String domainName = domain.getElementName();
		domainName = domainName.substring(0, domainName.indexOf('.'));
		String createFilePath = rootPath
				+ packageName.getPath().toFile().getPath() + File.separator
				+ domainName + "Service.java";
		Template.TemplateToFile(tempPath, createFilePath,
				packageName.getElementName(), daoName.getElementName(), null,
				null, null, domain, templateName,null);
	}

	// 创建domain的ServiceImpl tempPath模板文件的路径 createFilePath创建文件路径
	public static IPackageFragment createserviceImplBean(String tempPath,
			IPackageFragmentRoot packageFragmentRoot, IProject project,
			PackageFragment packageName, PackageFragment daoName,
			CompilationUnit domain, String templateName) throws Exception {
		String rootPath = project.getLocation().toFile().getParent();
		String domainName = domain.getElementName();
		domainName = domainName.substring(0, domainName.indexOf('.'));
		String createFilePath = rootPath
				+ packageName.getPath().toFile().getPath() + File.separator
				+ "impl" + File.separator + domainName + "ServiceImpl.java";
		IPackageFragment imp = packageFragmentRoot.createPackageFragment(
				packageName.getElementName() + ".impl", true, null);
		Template.TemplateToFile(tempPath, createFilePath, imp.getElementName(),
				daoName.getElementName(), packageName.getElementName(), null,
				null, domain, templateName,null);
		return imp;
	}

	// 创建domain的Action的实现类
	public static void createBeanAction(String tempPath, IProject project,
			PackageFragment packageName, PackageFragment daoName,
			IPackageFragment daoImp, PackageFragment service,
			IPackageFragment serviceImpl, PackageFragment facade,
			IPackageFragment facadeImpl, CompilationUnit domain,
			String templateName,String rootName) throws Exception {
		String rootPath = project.getLocation().toFile().getParent();
		String domainName = domain.getElementName();
		domainName = domainName.substring(0, domainName.indexOf('.'));
		String createFilePath = rootPath + packageName.getPath().toFile().getPath() + File.separator + domainName + "Action.java";
		String daoImpStr = "";
		String serviceStr = "";
		String serviceImplStr = "";
		String facadeStr = "";
		String facadeImplStr = "";

		if (null != daoImp) {
			daoImpStr = daoImp.getElementName();
		}
		if (null != service) {
			serviceStr = service.getElementName();
		}
		if (null != serviceImpl) {
			serviceImplStr = serviceImpl.getElementName();
		}
		if (null != facade) {
			facadeStr = facade.getElementName();
		}
		if (null != facadeImpl) {
			facadeImplStr = facadeImpl.getElementName();
		}

		Template.TemplateToFile(tempPath, createFilePath,
				packageName.getElementName(), daoName.getElementName(),
				daoImpStr, serviceStr, serviceImplStr, facadeStr,
				facadeImplStr, domain, templateName,rootName);
	}

	// 创建domain的bean类
	public static void createBean(IProject project,
			PackageFragment packageName, CompilationUnit domain,
			String templateName) throws Exception {
		String rootPath = project.getLocation().toFile().getParent();
		String domainName = domain.getElementName();
		domainName = domainName.substring(0, domainName.indexOf('.'));
		// 创建文件路径
		String createFilePath = rootPath + packageName.getPath().toFile().getPath() + File.separator + domainName + "Bean.java";
		Template.DomainToBeanFile(createFilePath, packageName.getElementName(),domain, templateName);
	}

	// 创建domain的facade tempPath模板文件的路径 createFilePath创建文件路径
	public static void createFacadeBean(String tempPath, IProject project,
			PackageFragment packageName, CompilationUnit domain,
			String templateName) throws Exception {
		String rootPath = project.getLocation().toFile().getParent();
		String domainName = domain.getElementName();
		domainName = domainName.substring(0, domainName.indexOf('.'));
		String createFilePath = rootPath + packageName.getPath().toFile().getPath() + File.separator + domainName + "Facade.java";
		
		
		Template.TemplateToFile(tempPath, createFilePath,
				packageName.getElementName(), null, null, null, null, domain,
				templateName,null);
	}

	public static void createFacadeImplBean(String tempPath,
			IProject project, PackageFragment dao,
			PackageFragment service, 
			PackageFragment facade,
			IPackageFragment facadeImpl, 
			CompilationUnit domain,
			String templateName) throws Exception {
		String rootPath = project.getLocation().toFile().getParent();
		String domainName = domain.getElementName();
		domainName = domainName.substring(0, domainName.indexOf('.'));
		String createFilePath = rootPath + facadeImpl.getPath().toFile().getPath() + File.separator + domainName + "FacadeImpl.java";
		String daoStr = "";
		String serviceStr = "";
		String serviceImplStr = "";
		String facadeStr = "";
		String facadeImplStr = "";

		if (null != dao) {
			daoStr = dao.getElementName();
		}
		if (null != service) {
			serviceStr = service.getElementName();
		}
		if (null != facade) {
			facadeStr = facade.getElementName();
		}
		if (null != facadeImpl) {
			facadeImplStr = facadeImpl.getElementName();
		}
		Template.TemplateToFile(tempPath, createFilePath,
				facadeImplStr, daoStr,
				serviceStr, facadeStr, facadeImplStr, domain,
				templateName,null);
	}

	// 创建domain的AddJsp
	public static void createBeanAddJsp(String tempPath, Folder jspPath,
			IProject project, IPackageFragmentRoot packageFragmentRoot,
			CompilationUnit domain,String rootName, String templateName) throws Exception {

		String rootPath = project.getLocation().toFile().getParent();
		String domainName = domain.getElementName();
		domainName = domainName.substring(0, domainName.indexOf('.'));
		System.out.println(project.getName());
		String folderPaths = rootPath + File.separator + project.getName()
				+ File.separator
				+ jspPath.getProjectRelativePath().toFile().getPath();
		String domainParent = domain.getParent().getElementName();
		String[] parentName = domainParent.split("\\.");
		String pName = parentName[parentName.length - 2];
		// 下面这个是未改过之前的
		// String createFile =
		// folderPaths+File.separator+packageFragmentRoot.getElementName()+File.separator+getLowercaseChar(domainName)+File.separator+getLowercaseChar(domainName)+"Add.jsp";
		String createFile = folderPaths + File.separator + pName
				+ File.separator + getLowercaseChar(domainName)
				+ File.separator + getLowercaseChar(domainName) + "Add.jsp";
		BeanUtils.newParentFolders(createFile);
		// List list = BeanUtils.getFields(domain, project);
		List list = Anno.parserContentFields(domain.getSource());
		Template.VeliocityJspToFile(list, templateName, tempPath, createFile,
				domain,rootName, "add.jsp");
	}

	// 创建domain的ListJsp
	public static void createBeanListJsp(String tempPath, Folder jspPath,
			IProject project, IPackageFragmentRoot packageFragmentRoot,
			CompilationUnit domain,String rootName, String templateName) throws Exception {
		String rootPath = project.getLocation().toFile().getParent();
		String domainName = domain.getElementName();
		domainName = domainName.substring(0, domainName.indexOf('.'));
		String folderPaths = rootPath + File.separator + project.getName()
				+ File.separator
				+ jspPath.getProjectRelativePath().toFile().getPath();
		String domainParent = domain.getParent().getElementName();
		String[] parentName = domainParent.split("\\.");
		String pName = parentName[parentName.length - 2];
		// 下面这个是未改过之前的
		// String createFile =
		// folderPaths+File.separator+packageFragmentRoot.getElementName()+File.separator+getLowercaseChar(domainName)+File.separator+getLowercaseChar(domainName)+"List.jsp";
		String createFile = folderPaths + File.separator + pName
				+ File.separator + getLowercaseChar(domainName)
				+ File.separator + getLowercaseChar(domainName) + "List.jsp";
		BeanUtils.newParentFolders(createFile);
		List list = Anno.parserContentFields(domain.getSource());
		// List list = BeanUtils.getFields(domain, project);
		Template.VeliocityJspToFile(list, templateName, tempPath, createFile,
				domain,rootName, "list.jsp");
	}

	/**
	 * 创建bean的ListJsp
	 * 
	 * @param c
	 * @throws Exception
	 */
	public static void createBeanUpdateJsp(String tempPath, Folder jspPath,
			IProject project, IPackageFragmentRoot packageFragmentRoot,
			CompilationUnit domain,String rootName, String templateName) throws Exception {
		String rootPath = project.getLocation().toFile().getParent();
		String domainName = domain.getElementName();
		domainName = domainName.substring(0, domainName.indexOf('.'));
		String folderPaths = rootPath + File.separator + project.getName()
				+ File.separator
				+ jspPath.getProjectRelativePath().toFile().getPath();
		String domainParent = domain.getParent().getElementName();
		String[] parentName = domainParent.split("\\.");
		String pName = parentName[parentName.length - 2];
		// 下面这个是未改过之前的
		// String createFile =
		// folderPaths+File.separator+packageFragmentRoot.getElementName()+File.separator+getLowercaseChar(domainName)+File.separator+getLowercaseChar(domainName)+"Update.jsp";
		String createFile = folderPaths + File.separator + pName
				+ File.separator + getLowercaseChar(domainName)
				+ File.separator + getLowercaseChar(domainName) + "Update.jsp";
		BeanUtils.newParentFolders(createFile);
		List list = Anno.parserContentFields(domain.getSource());
		// List list = BeanUtils.getFields(domain, project);
		Template.VeliocityJspToFile(list, templateName, tempPath, createFile,
				domain,rootName, "update.jsp");
	}

	// 创建jsp文件下边的文件夹
	public static void newParentFolders(String folderPath) {
		try {
			String filePath = folderPath;
			File myFilePath = new File(filePath);
			if (!myFilePath.getParentFile().exists()) {
				myFilePath.getParentFile().mkdirs();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 创建jsp文件下边的文件夹
	public static void newFolders(String folderPath) {
		try {
			String filePath = folderPath;
			File myFilePath = new File(filePath);
			if (!myFilePath.exists()) {
				myFilePath.mkdirs();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取路径的最后面字符串<br>
	 */
	public static String getLastChar(String str) {
		if ((str != null) && (str.length() > 0)) {
			int dot = str.lastIndexOf('.');
			if ((dot > -1) && (dot < (str.length() - 1))) {
				return str.substring(dot + 1);
			}
		}
		return str;
	}

	/**
	 * 把第一个字母变为小写<br>
	 */
	public static String getLowercaseChar(String str) {
		return str.substring(0, 1).toLowerCase() + str.substring(1);
	}

	/**
	 * 获取系统时间
	 * 
	 * @return
	 */
	public static String getDate() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return simpleDateFormat.format(new Date());
	}

	// 反射获取属性名
	public static List getFields(CompilationUnit domain, IProject project) {
		String domainName = domain.getElementName();
		domainName = domainName.substring(0, domainName.indexOf('.'));
		String fileNames = domain.getParent().getElementName() + "."
				+ domainName;
		Field[] fs = null;
		ClassLoaderFromIProjectHelper helper = new ClassLoaderFromIProjectHelper();
		// TODO 使用反射时会出问题
		fs = helper.findClass(fileNames, project).getDeclaredFields();
		// 解析出来的字段添加到list 集合
		List list = new ArrayList();
		for (int i = 0; i < fs.length; i++) {
			list.add(fs[i].getName());
		}
		return list;
	}
}
